// Copyright (c) MatrixFramework. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using Microsoft.EntityFrameworkCore;
using PhotoGalleryService.Photos;

namespace PhotoGalleryService.EntityFrameworkCore;

/// <summary>
/// PhotoGallery服务数据库模型配置扩展
/// </summary>
public static class PhotoGalleryServiceDbContextModelCreatingExtensions
{
    /// <summary>
    /// 配置PhotoGallery服务的数据库模型
    /// </summary>
    /// <param name="builder">模型构建器</param>
    public static void ConfigurePhotoGalleryService(this ModelBuilder builder)
    {
        builder.ConfigurePhoto();
    }

    /// <summary>
    /// 配置Photo实体
    /// </summary>
    /// <param name="builder">模型构建器</param>
    private static void ConfigurePhoto(this ModelBuilder builder)
    {
        builder.Entity<Photo>(b =>
        {
            b.ToTable("Photos");

            // 配置主键
            b.HasKey(x => x.Id);

            // 配置属性
            b.Property(x => x.FileName)
                .IsRequired()
                .HasMaxLength(255);

            b.Property(x => x.OriginalFileName)
                .IsRequired()
                .HasMaxLength(500);

            b.Property(x => x.FilePath)
                .IsRequired()
                .HasMaxLength(1000);

            b.Property(x => x.FileSize)
                .IsRequired();

            b.Property(x => x.MimeType)
                .IsRequired()
                .HasMaxLength(100);

            b.Property(x => x.FileHash)
                .IsRequired()
                .HasMaxLength(64);

            b.Property(x => x.Title)
                .HasMaxLength(500);

            b.Property(x => x.Description)
                .HasMaxLength(2000);

            b.Property(x => x.Location)
                .HasMaxLength(500);

            b.Property(x => x.CameraModel)
                .HasMaxLength(200);

            b.Property(x => x.Tags)
                .HasMaxLength(1000);

            b.Property(x => x.ThumbnailPath)
                .HasMaxLength(1000);

            b.Property(x => x.MediumPath)
                .HasMaxLength(1000);

            // 配置索引
            b.HasIndex(x => x.FileHash)
                .IsUnique();
        });
    }
}